package com.sphd.beautysalon.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.presenter.JifenRechageSettingPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.JiFenGiveInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.view.JifenRechageSettingView;
import com.wzh.frame.utils.StringUtils;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 充值积分
 */
public class JifenRechageSettingActivity extends BaseActivity implements JifenRechageSettingView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.text_chongzhi)
    TextView textChongzhi;
    @BindView(R.id.rechage_getjifen_switch)
    Switch rechageGetjifenSwitch;
    @BindView(R.id.restaurant_pay_up_layout)
    RelativeLayout restaurantPayUpLayout;
    @BindView(R.id.rechage_edit)
    MaterialEditText rechageEdit;
    @BindView(R.id.getjifen_edit)
    MaterialEditText getjifenEdit;
    @BindView(R.id.jifen_finish_btn)
    Button jifenFinishBtn;
    @BindView(R.id.jifen_layout)
    LinearLayout jifenLayout;
    @BindView(R.id.content_jifen_rechage_setting)
    LinearLayout contentJifenRechageSetting;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    JifenRechageSettingPresenterImpl jifenRechageSettingPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jifen_rechage_setting);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> {
            setResult(0); finish();
        });
        rechageGetjifenSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (!b){
                    jifenRechageSettingPresenter.setSwitchRes(RestaurantUrl.Httptitle +
                            RestaurantUrl.ADD_INTEGRAL_TOPUP + "cgid=" + restaurantInfo.getId()
                            + "&status=" + (rechageGetjifenSwitch.isChecked() ? "1" : "0") +
                            "&money="
                            + "&integral=");
                }
                setSwitch();
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "积分充值设置");
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        rechageGetjifenSwitch.setChecked(restaurantInfo.getCg_integral_topup() == 1 ? true : false);
        jifenRechageSettingPresenter = new JifenRechageSettingPresenterImpl(this);
        setSwitch();
    }

    private void setSwitch() {
        if (rechageGetjifenSwitch.isChecked()) {
            jifenLayout.setVisibility(View.VISIBLE);
            addData();
        } else {
            jifenLayout.setVisibility(View.GONE);
        }
    }

    private void addData() {
        jifenRechageSettingPresenter.addData(RestaurantUrl.Httptitle +
                RestaurantUrl.TOPUP_SPEND_INTEGRAL + "cgid=" + restaurantInfo.getId() + "&type=0");

    }

    @OnClick(R.id.jifen_finish_btn)
    public void onClick() {
            if (StringUtils.isEmpty(rechageEdit.getText().toString().trim())||StringUtils.isEmpty(getjifenEdit.getText().toString().trim())){
                toast("积分设置不能有空的选项");
            }else {
                jifenRechageSettingPresenter.setSwitchRes(RestaurantUrl.Httptitle +
                        RestaurantUrl.ADD_INTEGRAL_TOPUP + "cgid=" + restaurantInfo.getId()
                        + "&status=" + (rechageGetjifenSwitch.isChecked() ? "1" : "0") +
                        "&money=" + rechageEdit.getText().toString()
                        + "&integral=" + getjifenEdit.getText().toString());
            }
    }

    @Override
    public void recJiFenGiveInfo(JiFenGiveInfo jiFenGiveInfo) {
        if (jiFenGiveInfo != null) {
            rechageEdit.setText(String.valueOf(jiFenGiveInfo.getMoney()));
            getjifenEdit.setText(String.valueOf(jiFenGiveInfo.getIntegral()));
        } else {
            rechageEdit.setText("");
            getjifenEdit.setText("");
        }
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("提交成功");
            setSwitch();
        } else {
            toast("提交失败，请重试！");
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}

